package com.itzx.hotitems_analysis

import java.util.Properties

import org.apache.kafka.clients.producer.{KafkaProducer, ProducerRecord}

import scala.io.BufferedSource



/**
 * kafka生产者
 *
 * author: yyeleven
 * create: 2020/3/22 10:50
 */
object KafkaProducer {

  def main(args: Array[String]): Unit = {
    writeToKafka("hotitems")
  }

  def writeToKafka(topic: String): Unit = {

    val properties: Properties = new Properties()
    properties.setProperty("bootstrap.servers", "node-1:9092,node-2:9092,node-3:9092")
    properties.setProperty("key.serializer", "org.apache.kafka.common.serialization.StringSerializer")
    properties.setProperty("value.serializer", "org.apache.kafka.common.serialization.StringSerializer")

    //定义一个kafka 生产者
    val producer: KafkaProducer[String, String] = new KafkaProducer[String, String](properties)

    // 从文件中读取数据,发送到kafka
    val bufferSource: BufferedSource = io.Source.fromFile("E:\\workroom_own\\UserBehaviorAnalysis\\HotitemsAnalysis\\src\\main\\resources\\UserBehavior.csv")
    for (line <- bufferSource.getLines()) {
      val record: ProducerRecord[String, String] = new ProducerRecord[String, String](topic, line)
      Thread.sleep(1)
      producer.send(record)
    }
  }
}
